import pagesJSON from '@/pages.json'

/**
 * 用户反馈（轻提示）
 * @param title 提示文本内容
 * @param icon 提示图标类型：success,error,fail,exception,loading,none
 */
export const toast = (title = '数据加载失败！', icon = 'none', mask = false) => {
	return uni.showToast({
		title,
		icon,
		// mask: true, // 是否添加透明遮罩，防止点击后面的区域
		mask,
	})
}

/**
 * 获取当前页面的路径
 * @param needSlash 是否需要以斜杠开头，默认值：true
 */
export const getCurrentPageRoute = (needSlash = true) => {
	const pageStack = getCurrentPages()
	const currentPage = pageStack[pageStack.length - 1]
	if (currentPage.$page.options.id) {
		return (needSlash ? '/' : '') + currentPage.route + '?id=' + currentPage.$page.options.id
	} else {
		return (needSlash ? '/' : '') + currentPage.route
	}
}

/**
 * 获取tabBar页面路径数组
 */
export const getTabBarPagePathList = () => {
	return pagesJSON.tabBar.list.map((v) => v.pagePath)
}

// 方便全局进行引用
uni.toast = toast
uni.getCurrentPageRoute = getCurrentPageRoute
uni.getTabBarPagePathList = getTabBarPagePathList